// Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
//
// This file is part of FileBow.
//
// FileBow is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FileBow is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FileBow.  If not, see <http://www.gnu.org/licenses/>.
//
// Implementation of TestDrive.

#include "testing/test_drive.h"

#include <Poco/File.h>
#include <Poco/Path.h>

#include "common/common.h"

using namespace std;
using Poco::File;
using Poco::Path;

namespace filebow {
namespace testing {

const char* kTestFiles[] = {
  "file1",
  "file2",
  "file3",
  "file4",
  "project/file11",
  "project/dir11/file111",
  "project/dir11/file112",
  "project/dir11/dir111/file1111",
  "project/dir11/dir111/file1112",
  "project/dir12/file121",
  "project/dir12/file122",
  "project/dir12/file123.h",
  "project/dir12/file123.cpp",
  "project/dir12/file123_test.cpp",
  "project/dir12/file124",
  "project/dir13/file131",
  "project/dir13/file132",
  "project/dir13/file133",
  "global/file21",
  "global/file22",
};
const int kTestFilesCount = sizeof(kTestFiles) / sizeof(const char*);

TestDrive::TestDrive() {
  Path drivePath(Path::temp());
  drivePath.append("d57a4de16197399b80e6028c38a581a4");
  mPath = drivePath.toString();
  SafeRemove();
  for (int i = 0; i < kTestFilesCount; ++i) {
    Path filePath(drivePath);
    filePath.append(kTestFiles[i]);
    File(filePath.parent()).createDirectories();
    CHECK(File(filePath).createFile());
  }
  LOG(INFO) << "Installed TestDrive in: " << mPath;
}

TestDrive::~TestDrive() {
  SafeRemove();
}

int TestDrive::size() const {
  return kTestFilesCount;
}

void TestDrive::SafeRemove() {
  File pathFile(mPath);
  if (pathFile.exists()) {
    pathFile.remove(true);
    LOG(INFO) << "TestDrive removed";
  }
}

}  // namespace testing
}  // namespace filebow
